<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePatrolTaskTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //车辆信息 车辆验收表
        Schema::create('car_check', function (Blueprint $table) {
            $table->Integer('id')->unsigned()->autoIncrement();
            $table->string('reason')->comment('拒收原因')->comment('暂无')->nullable();
            $table->timestamp('accept_time')->comment('接受时间')->nullable();
            $table->boolean('is_accept')->comment('是否接受')->default(false);
            $table->boolean('is_compete')->comment('证件是否齐全')->default(false);
            $table->boolean('is_ok')->comment('点检是否有问题')->default(false);
            $table->boolean('has_accident')->comment('是否有事故')->default(false);
            $table->boolean('has_punish')->comment('是否有处罚')->default(false);
            $table->unsignedInteger('status')->comment('状态 1. 待交接 2. 拒收 3. 可用')->default(1);
            $table->bigInteger('car_id')->unsigned()->comment('接受车辆')->nullable()->unique();
            $table->foreign('car_id')->on('cars')->references('id')->onDelete('set null');
            $table->timestamp('year_check')->comment('年检日期')->nullable();
            $table->integer('car_status')->comment('车辆状况 0.良好 1.待维修')->default(0);
            $table->timestamps();
        });

        //验收表的文件
        Schema::create('car_check_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedInteger('check_id')->comment('对应验收信息')->nullable();
            $table->foreign('check_id')->on('car_check')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['check_id','file_id'],'bf_unique');
        });

//        //任务类型 一级
//        Schema::create('patrol_type_first', function (Blueprint $table) {
//            $table->bigIncrements('id');
//            $table->string('type_name')->comment('任务类型一级名称');
//            $table->timestamps();
//        });
//        //任务类型 二级
//        Schema::create('patrol_type_second', function (Blueprint $table) {
//            $table->bigIncrements('id');
//            $table->string('type_name')->comment('任务类型二级名称');
//            $table->unsignedBigInteger('pid')->comment('任务一级名称')->nullable();
//            $table->foreign('pid')->references('id')->on('patrol_type_first')->onDelete('CASCADE');
//            $table->timestamps();
//        });
        //巡查问题类型
        Schema::create('patrol_type', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('name')->comment('名称')->nullable();
            $table->unsignedBigInteger('parent_id')->comment('父级id')->nullable();
            $table->timestamps();
        });
        //巡查任务
        Schema::create('patrol_task', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('patrol_address')->comment('巡查位置');
            $table->unsignedInteger('task_type')->comment('任务类型')->nullable();
            $table->foreign('task_type')->references('id')->on('task_categories')->onDelete('set null');
            $table->unsignedInteger('task_type_second')->comment('任务类型二级')->nullable();
            $table->foreign('task_type_second')->references('id')->on('task_categories')->onDelete('set null');
            $table->unsignedInteger('employee')->comment('人员')->nullable();
            $table->foreign('employee')->references('id')->on('employees')->onDelete('set null');
            $table->string('car')->comment('车辆信息')->nullable();
         //   $table->foreign('car')->references('id')->on('cars')->onDelete('set null');
            $table->string('patrol_tool')->comment('巡查工具');
            $table->string('task_summary')->comment('任务概要');
            $table->string('task_body')->comment('任务内容');
            $table->text('body')->comment('注意事项');
            $table->tinyInteger('clean')->comment('清洁度 0,1,2,3,4,5')->nullable();
            $table->tinyInteger('major')->comment('专业 0,1,2,3,4,5')->nullable();
            $table->tinyInteger('speed')->comment('速度 0,1,2,3,4,5')->nullable();
            $table->integer('score')->comment('评分 0<x<100')->nullable();
            $table->text('opinion')->comment('意见')->nullable();
            $table->unsignedBigInteger('voice')->comment('巡查结果 语音')->nullable();
            $table->foreign('voice')->references('id')->on('file_upload')->onDelete('set null');
            $table->text('other')->comment('其他')->nullable();
            $table->tinyInteger('task_level')->comment('紧急程度 一级 二级 三级')->default(1);
            $table->tinyInteger('status')->comment('状态值 0待完成，1完成')->default(0);
            $table->unsignedBigInteger('type')->comment('问题类型')->nullable();
            $table->foreign('type')->references('id')->on('patrol_type')->onDelete('set null');
            $table->timestamps();
        });
        Schema::create('patrol_img', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('patrol_task_id')->comment('巡查任务id')->nullable();
            $table->unsignedBigInteger('img_id')->comment('图片id')->nullable();
            $table->foreign('patrol_task_id')->references('id')->on('patrol_task')->onDelete('CASCADE');
            $table->foreign('img_id')->references('id')->on('file_upload')->onDelete('CASCADE');
            $table->timestamps();
        });

        //区域人员配置
         Schema::create('sanitation_staff', function (Blueprint $table) {
             $table->bigIncrements('id');
             $table->unsignedInteger('scope_id')->comment('道路id')->nullable();
             $table->unsignedInteger('employee_id')->comment('人员id')->nullable();
             $table->unsignedInteger('type')->comment('任务类型')->nullable();
             $table->foreign('type')->references('id')->on('task_categories')->onDelete('set null');
             $table->foreign('scope_id')->references('id')->on('job_scopes')->onDelete('set null');
             $table->foreign('employee_id')->references('id')->on('employees')->onDelete('set null');
             $table->timestamps();
         });
        //园林区域人员配置
        Schema::create('green_staff', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('landscaping_id')->comment('园林id')->nullable();
            $table->unsignedInteger('department_id')->comment('对应人员组id')->nullable();
            $table->foreign('landscaping_id')->references('id')->on('landscapings')->onDelete('set null');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->timestamps();
        });
        //绿化任务量分析
        Schema::create('green_task', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('date')->comment('日期');
            $table->string('task_type')->comment('任务类型');
            $table->string('vegetation_type')->comment('植被种类');
            $table->string('number')->comment('数量');
            $table->string('company')->comment('单位');
            $table->dateTimeTz('start_time')->comment('开始时间');
            $table->dateTimeTz('finish_time')->comment('完成时间');
            $table->string('time')->comment('工作时长');
            $table->string('department')->comment('负责组');
            $table->string('employee_leader')->comment('负责人');
            $table->string('employee')->comment('成员');
            $table->string('level')->comment('等级');
            $table->string('day_work')->comment('日工作量');
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
//        Schema::dropIfExists('patrol_type_second');
//        Schema::dropIfExists('patrol_type_first');
        Schema::dropIfExists('green_task');
        Schema::dropIfExists('green_staff');
        Schema::dropIfExists('sanitation_staff');
        Schema::dropIfExists('patrol_type');
        Schema::dropIfExists('patrol_task');
        Schema::dropIfExists('car_check');
        Schema::dropIfExists('car_check_files');


    }
}
